package com.zhou.bjgl.bjgl.util;

import cn.hutool.Hutool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: DateUtil
 * @Description:
 * @Author: zhou
 * @Date: 2022/4/22 11:33
 */
public class DateUtil {
    /**
     *  获取两个日期之间的所有日期 (年月日)
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getBetweenDate(String startTime, String endTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime()<=endDate.getTime()){
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate=calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<String> getLastWeek() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar instance = Calendar.getInstance();
        String format = sdf.format(instance.getTime());

        List<String> result = new ArrayList<>();
        try {
            Date endDate = sdf.parse(format);
            instance.setTime(endDate);
            instance.add(Calendar.DATE,-6);
            Date startDate = instance.getTime();
            String start = sdf.format(startDate);
            String end = sdf.format(endDate);
            result=getBetweenDate(start,end);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static void main(String[] args) {
        List<String> lastWeek = getLastWeek();
        for (String s : lastWeek) {
            System.out.println(s);
        }
    }

}
